home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch21
/
fig21_15.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
1KB
|
46 lines
1 // Fig. 21.15: fig21_15.cpp
2 // Attempting to polymorphically call a function
3 // multiply inherited from two base classes.
4 #include <iostream.h>
5
6 class Base {
7 public:
8 virtual void print() const = 0; // pure virtual
9 };
10
11 class DerivedOne : public Base {
12 public:
13 // override print function
14 void print() const { cout << "DerivedOne\n"; }
15 };
16
17 class DerivedTwo : public Base {
18 public:
19 // override print function
20 void print() const { cout << "DerivedTwo\n"; }
21 };
22
23 class Multiple : public DerivedOne, public DerivedTwo {
24 public:
25 // qualify which version of function print
26 void print() const { DerivedTwo::print(); }
27 };
28
29 int main()
30 {
31 Multiple both; // instantiate Multiple object
32 DerivedOne one; // instantiate DerivedOne object
33 DerivedTwo two; // instantiate DerivedTwo object
34
35 Base *array[ 3 ];
36 array[ 0 ] = &both; // ERROR--ambiguous
37 array[ 1 ] = &one;
38 array[ 2 ] = &two;
39
40 // polymorphically invoke print
41 for ( int k = 0; k < 3; k++ )
42 array[ k ] -> print();
43
44 return 0;
45 }